public class ColorDetector {

	public static final int SAMPLE_SIZE = 5;

	public static void initialize() {
		double value = readLightMean();
		Constants.EMPTY_FLOOR_COLOR += value;
		Constants.FIRST_ROOM_COLOR += value;
		Constants.SECOND_ROOM_COLOR += value;
		Constants.KING_COLOR += value;
	}

	public static double readLightMean() {
	try {
		Thread.sleep(300);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
		double sum = 0;
		for (int i = 0; i < SAMPLE_SIZE; i++)
			sum += Robot.robothw.readLight();
		return sum / 5;
	}

	public static int detectColor() {
		double value = readLightMean();
		int type = Constants.EMPTY_FLOOR;
		double minDiff = 1000;
		double diff;
		
		diff = Math.abs(Constants.EMPTY_FLOOR_COLOR - value);
		if ( diff < minDiff){
			type = Constants.EMPTY_FLOOR;
			minDiff = diff;
		}
		diff = Math.abs(Constants.FIRST_ROOM_COLOR - value);
		if ( diff < minDiff){
			type = Constants.FIRST_ROOM;
			minDiff = diff;
		}
		diff = Math.abs(Constants.SECOND_ROOM_COLOR - value);
		if ( diff < minDiff){
			type = Constants.SECOND_ROOM;
			minDiff = diff;
		}		
		diff = Math.abs(Constants.KING_COLOR - value);
		if ( diff < minDiff){
			type = Constants.KING;
			minDiff = diff;
		}	
		return type;
	}
}
